package com.ruoyi.common.utils;



import com.ruoyi.common.exception.ClientException;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

public class CommonUtil {

    private static final SimpleDateFormat FORMATTER_YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");

    /**
     * 判断是否为空
     */
    public static boolean isEmpty(Object value) {
        if (value == null)
            return true;

        if (value instanceof String)
            return ((String) value).trim().isEmpty();

        if (value instanceof Collection)
            return ((Collection) value).isEmpty();

        if (value instanceof Map)
            return ((Map) value).isEmpty();
        return false;
    }

    /**
     * 字符串拼接 <br>
     * (输入参数最好最是字符串，如果不是字符串那么其长度最好不好超过16)<br>
     * <br><br>
     * String.format 可以格式化输出，但是效率最差<br>
     * + 效率次之<br>
     * StringBuilder.append 效率最好，但使用麻烦<br>
     */
    public static String append(Object... args) {
        if (args == null || args.length == 0)
            return null;
        if (args.length == 1 && args[0] instanceof String)
            return (String) args[0];

        // 初使容量计算
        int capacity = 0;
        for (Object s : args) {
            if (s == null)
                capacity += 5;

            else if (s instanceof String)
                capacity += ((String) s).length();
                // 输入参数最好最是字符串，如果不是字符串那么其长度最好不好超过16
            /*else if (s instanceof Integer)
                capacity += 11;*/
            else
                capacity += 16;
        }

        StringBuilder sb = new StringBuilder(capacity);
        for (Object s : args) {
            if (s instanceof Date)
                sb.append(getTime((Date) s));
            else
                sb.append(s);
        }
        return sb.toString();
    }

    /** 把时间转为字符串 */
    public static String getTime(Date date)
    {
        if (date == null)
            return null;
        if (date != null)
        {
            return FORMATTER_YYYY_MM_DD_HH_MM_SS.format(date);
        }
        return null;
    }

    public static String whoCalledMe() {
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        StackTraceElement caller = stackTraceElements[3];
        String filename = caller.getFileName();
        int lineNumber = caller.getLineNumber();
        return append(" at (", filename, ":", lineNumber, ") ");
    }

    /**
     * 期望条件必真，否则抛出异常给客户端
     */
    public static void assertTrue(boolean truth, Object... message) {
        if (truth)
            return;
        throw new ClientException(400, message);
    }

    /**
     * 不足N位，前面补0
     */
    public static String appendZero(long number, int lenth) {
        StringBuilder sb = new StringBuilder(lenth);
        sb.append(number);
        while (sb.length() < lenth) {
            sb.insert(0, '0');
        }
        return sb.toString();
    }

    /** 姓名脱敏 */
    public static String nameDesensitization(String name) {
        if (name != null && name.length() > 2) {
            return CommonUtil.append(name.charAt(0), "*", name.charAt(name.length()-1));
        }
        if (name != null && name.length() == 2) {
            return CommonUtil.append("**", name.charAt(name.length()-1));
        }
        return name;
    }
}
